package com.github.cyf.util.video;

import cn.hutool.core.collection.CollUtil;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author chenyifan
 * @create 2024-10-05 22:57
 */
public class FramePlay extends JFrame {

    private String frameDir;
    private List<ImageIcon> frames;
    private JLabel imageLabel = new JLabel("图片控件");

    public FramePlay(String frameDir) {
        this.frameDir = frameDir;
    }

    public void start() throws IOException {
        // 加载图片
        loadImage();
        // 设置窗口大小
        setSize(700, 500);
        // 居中显示
        setLocationRelativeTo(null);
        // 设置可见性
        setVisible(true);
        // 设置关闭退出
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 添加组件
        initComponents();
    }

    private void initComponents() {
        // 内容面板设置为无布局
        JPanel panel = (JPanel) getContentPane();
        panel.setLayout(null);
        // 设置图片控件的边界
        imageLabel.setBounds(0, 0, 700, 500);
        // 播放图片
        Thread thread = new Thread(() -> {
            for (ImageIcon frame : frames) {
                imageLabel.setIcon(frame);
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread.start();
        // 面板内容添加图片控件
        panel.add(imageLabel);
    }

    /**
     * 加载图片时使用多线程，加快加载速度
     */
    private void loadImage() {
        File dir = new File(frameDir);
        List<File> files;
        try (Stream<Path> paths = Files.walk(dir.toPath())) {
            files = paths.filter(Files::isRegularFile).map(Path::toFile).collect(Collectors.toList());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        this.frames = new ArrayList<>(files.size());
        int splitSize = 5;
        List<List<File>> splitList = CollUtil.splitList(files, splitSize);
        List<CompletableFuture<List<ImageIcon>>> futures = new ArrayList<>();
        for (int i = 0; i < splitSize; i++) {
            List<File> list = splitList.get(i);
            CompletableFuture<List<ImageIcon>> future = CompletableFuture.supplyAsync(() -> {
                List<ImageIcon> images = new ArrayList<>();
                for (File file : list) {
                    images.add(new ImageIcon(file.getPath()));
                }
                return images;
            });
            futures.add(future);
        }

        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));

        try {
            for (CompletableFuture<List<ImageIcon>> future : futures) {
                this.frames.addAll(future.get());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
